import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
//通过URL下载文件(用字节流)
public class URLDemo {
    public static void main(String[] args) throws IOException {
        //1.下载地址
        URL url = new URL("https://c-ssl.duitang.com/uploads/blog/202103/14/20210314194435_c5822.thumb.400_0.jpg");
        //2.连接到这个资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\JavaLearn\\idea\\Code\\JavaSE_Code\\MyNet\\src\\tmp\\image.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = inputStream.read(buffer)) != -1){
            bufferedOutputStream.write(buffer,0,len);
        }
        bufferedOutputStream.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
